﻿using NextInventory.Business.EntityRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NextInventory.Model.Menufacturer;
using MvcJqGrid;
namespace NextInventory.Web.Controllers
{
    [Authorize]
    public class ManufacturerController : Controller
    {
        private IMenufacturerRepository _menufacturer;

        public ManufacturerController(IMenufacturerRepository menufacturer)
        {
            _menufacturer = menufacturer;
        }

        public ActionResult Index()
        {

            return View();
        }

        #region "Manufacturer"

        [HttpGet]
        public ActionResult MenufacturerDetails(int? id)
        {
            return PartialView(id.HasValue ? _menufacturer.GetMenufacturerByID(id.Value) : new MenufacturerModel());
        }

        [HttpPost]
        public ActionResult MenufacturerDetails(MenufacturerModel model)
        {
            if (ModelState.IsValid)
            {
                bool isSuccess;
                if (model.MenufacturerId == 0)
                {
                    isSuccess = _menufacturer.AddMenufacturer(model);
                    if (isSuccess)
                    {
                        TempData["message"] = "";
                        ModelState.Clear();
                        return RedirectToAction("Menufacturer", "Menufacturer");
                    }
                    else
                    {

                    }
                }
                else
                {
                    isSuccess = _menufacturer.UpdateMenufacturer(model);
                    if (isSuccess)
                    {
                        TempData["message"] = "";
                        ModelState.Clear();
                        return RedirectToAction("Menufacturer", "Menufacturer");
                    }
                }
            }
            else
            {

            }
            return PartialView(model);
        }

        public ActionResult DeleteMenufacturer(int id)
        {
            if (_menufacturer.DeleteMenufacturer(id))
            {
                return RedirectToAction("Menufacturer", "Manufacturer");
            }
            return View();
        }

        public ActionResult Menufacturer()
        {
            return View();
        }
        public JsonResult MenufacturerGrid(GridSettings gridSettings)
        {
            List<MenufacturerModel> menufacturerList = _menufacturer.SearchMenufacturer();
            var totalCount = menufacturerList.Count();

            var jsonData = new
            {
                total = totalCount / gridSettings.PageSize + 1,
                page = gridSettings.PageIndex,
                records = totalCount,
                rows = (
                                           from m in menufacturerList
                                           select new
                                           {
                                               ID = m.MenufacturerId,
                                               Name = m.Name,
                                               Phone = m.Phone,
                                               Email = m.Email,
                                               Address = m.Address,
                                               City = m.City,
                                               Country = m.Country,

                                           }
                                           )

            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
